package bishi.alibaba;

import java.util.Arrays;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/10/09 19:23
 */
public class dapai {
    public static void main(String[] args) {
        System.out.println(dapai(new int[]{1,1,2,2,2,2,2,2,2,1}));
    }

    /**
     * @param nums
     * @return
     */
    public static int dapai(int[] nums){
        return lianpai(nums,0);
    }
    public static int lianpai(int[] nums,int daan){
        int liandui = 0;
        int shunzi = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]>=2){
                liandui++;
                shunzi++;
            }else if (nums[i]==1){
                shunzi++;
            }else{
                liandui=0;
                shunzi=0;
            }
            int[] copyNums1;
            int[] copyNums2;
            //同时遇见连对和顺子，就分支求解
            if (shunzi==5&&liandui==3){
                copyNums1 = Arrays.copyOf(nums,nums.length);
                copyNums1[i]-=1;
                copyNums1[i-1]-=1;
                copyNums1[i-2]-=1;
                copyNums1[i-3]-=1;
                copyNums1[i-4]-=1;
                copyNums2 = Arrays.copyOf(nums,nums.length);
                copyNums2[i]-=2;
                copyNums2[i-1]-=2;
                copyNums2[i-2]-=2;
                return Math.min(lianpai(copyNums1,daan+1),lianpai(copyNums2,daan+1));
            }
            //打出顺子
            if (shunzi==5){
                nums[i]-=1;
                nums[i-1]-=1;
                nums[i-2]-=1;
                nums[i-3]-=1;
                nums[i-4]-=1;
                //打出之后重新洗牌
                return lianpai(nums,daan+1);
            }
            //打出连对
            if (liandui==3){
                nums[i]-=2;
                nums[i-1]-=2;
                nums[i-2]-=2;
                //打出之后重新洗牌
                return lianpai(nums,daan+1);
            }
        }
        //打出全部的对子和单牌
        for (int num : nums) {
            daan += num % 2;
            daan += num / 2;
        }
        return daan;
    }
}
